package org.mx.spring.i18n;

import org.mx.StringUtils;
import org.mx.TypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.core.env.Environment;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述： 根据配置创建I18N的消息资源工厂
 *
 * @author john peng
 * Date time 2019/8/15 9:11 AM
 */
public class I18nResourceBundleFactory {
    private static final Logger logger = LoggerFactory.getLogger(I18nResourceBundleFactory.class);
    private List<AbstractMessageSource> messageSources = new ArrayList<>();

    private Environment env;
    private ApplicationContext context;

    /**
     * 默认的构造函数
     *
     * @param env     Spring IoC容器上下文环境
     * @param context Spring IoC容器上下文
     */
    public I18nResourceBundleFactory(Environment env, ApplicationContext context) {
        super();
        this.env = env;
        this.context = context;
    }

    /**
     * 获取加载的消息资源包列表
     *
     * @return 消息资源包列表
     */
    public List<AbstractMessageSource> getMessageSources() {
        return messageSources;
    }

    /**
     * 初始化工厂
     */
    public void init() {
        if (logger.isInfoEnabled()) {
            logger.info("Initialize all the i18n message resources.");
        }
        String providersStr = env.getProperty("i18n.providers");
        if (StringUtils.isBlank(providersStr)) {
            if (logger.isWarnEnabled()) {
                logger.warn("There are not any i18n message resource be defined.");
            }
            return;
        }
        String strReloadTimeout = env.getProperty("i18n.reloadTimeout", "180 SEC");
        long reloadTimeout = TypeUtils.string2TimePeriod(strReloadTimeout, 180 * 1000);
        String[] providerNames = StringUtils.split(providersStr, ",", true, true);
        for (String providerName : providerNames) {
            I18nMessageResourceCreator creator = null;
            if ("properties".equalsIgnoreCase(providerName)) {
                creator = new PropertiesI18nMessageResourceCreator(env, reloadTimeout);
            } else if ("dsdb".equalsIgnoreCase(providerName)) {
                String beanName = env.getProperty("i18n.dsdb.beanName");
                if (!StringUtils.isBlank(beanName)) {
                    creator = context.getBean(beanName, I18nMessageResourceCreator.class);
                }
            }
            if (creator != null) {
                AbstractMessageSource messageSource = creator.create();
                if (messageSource != null) {
                    messageSources.add(messageSource);
                }
            }
        }

    }

    /**
     * 销毁资源
     */
    public void destroy() {
        if (logger.isInfoEnabled()) {
            logger.info("Destroy all the i18n message resources.");
        }
        if (messageSources != null && !messageSources.isEmpty()) {
            messageSources.clear();
            messageSources = null;
        }
    }
}
